using System;
using System.Collections.Generic;
using System.Text;

namespace MoGameLib
{
    public class TileCollection<T> : ICollection<T>
    {
        private int m_Width;

        private int m_Height;

        public TileCollection(int width, int height)
        {
            m_Width = width;
            m_Height = height;
            Clear();
        }

        #region ICollection<T> Members

        T[,] m_Tiles;

        public void Add(T item)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void Clear()
        {
            m_Tiles = new T[m_Height, m_Width];
        }

        public bool Contains(T item)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            m_Tiles.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return m_Tiles.Length; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(T item)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            foreach (T tile in m_Tiles)
                yield return tile;
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        public T this[int x, int y]
        {
            get
            {
                if (x < 0 || x >= m_Width || y < 0 || y >= m_Height)
                    return default(T);

                return m_Tiles[y, x];
            }

            set
            {
                if (x >= 0 && x < m_Width && y >= 0 && y < m_Height)
                    m_Tiles[y, x] = value;
            }
        }
    }
}
